package com.ultrasoft.farmasoft.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Country;
import com.ultrasoft.farmasoft.domain.Locality;
import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.Pharmacystock;
import com.ultrasoft.farmasoft.domain.Provider;
import com.ultrasoft.farmasoft.domain.Providerpharmacy;
import com.ultrasoft.farmasoft.domain.Region;
import com.ultrasoft.farmasoft.domain.Stock;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.repository.CountryDao;
import com.ultrasoft.farmasoft.repository.LocalityDao;
import com.ultrasoft.farmasoft.repository.PharmacyDao;
import com.ultrasoft.farmasoft.repository.PharmacyStockDao;
import com.ultrasoft.farmasoft.repository.ProviderDao;
import com.ultrasoft.farmasoft.repository.ProviderPharmacyDao;
import com.ultrasoft.farmasoft.repository.RegionDao;
import com.ultrasoft.farmasoft.repository.StockDao;
import com.ultrasoft.farmasoft.repository.TransactionDao;
import com.ultrasoft.farmasoft.repository.UserDao;
import com.ultrasoft.farmasoft.repository.UserPharmacyDao;

@Service
public class CustomersServiceImpl implements CustomersService {
	
	private StockDao stockDao;
	private PharmacyDao pharmacyDao;
	private PharmacyStockDao pharmacyStockDao;
	private CountryDao countryDao;
	private RegionDao regionDao;
	private LocalityDao localityDao;
	private ProviderDao providerDao;
	private ProviderPharmacyDao providerPharmacyDao;
	private UserPharmacyDao userPharmacyDao;
	private UserDao userDao;
	private TransactionDao transactionDao;
	
	@Autowired
	public CustomersServiceImpl(TransactionDao transactionDao,UserDao userDao,UserPharmacyDao userPharmacyDao,ProviderPharmacyDao providerPharmacyDao,ProviderDao providerDao,LocalityDao localityDao,RegionDao regionDao,CountryDao countryDao,PharmacyDao pharmacyDao,StockDao stockDao,PharmacyStockDao pharmacyStockDao) {
		this.pharmacyStockDao 	= pharmacyStockDao;
		this.stockDao 			= stockDao;
		this.pharmacyDao 		= pharmacyDao;
		this.countryDao 		= countryDao;
		this.regionDao 			= regionDao;
		this.localityDao 		= localityDao;
		this.providerDao 		= providerDao;
		this.providerPharmacyDao= providerPharmacyDao;
		this.userPharmacyDao	= userPharmacyDao;
		this.userDao 			= userDao;
		this.transactionDao 	= transactionDao;
	}
	@Transactional
	public void addProviderPharmacy(Providerpharmacy providerPharmacy){
		providerPharmacyDao.add(providerPharmacy);
	}
	
	
	@Transactional 
	public void addProvider(Provider provider){
		providerDao.add(provider);
	}	

	
	@Transactional(readOnly = true)   
    public Country searchCountryByCountryId(Short countryId){
    	return countryDao.findCountryByID(countryId);
    }   
	@Transactional(readOnly = true)   
	public List<Country> getListCountry(){
		return countryDao.getCountryList();
	}	
	
	
	@Transactional(readOnly = true)   
	public List<Region> getListRegion(short countryId){
		return regionDao.getRegionList(countryId);
	}	
	@Transactional(readOnly = true)   
	public List<Region> getListRegionByCountryIdInLocality(short countryId){
		List<Region> temp = new ArrayList<Region>(); 
		List<Region> listRegion = regionDao.getRegionList(countryId);
		for (Region region : listRegion) {
			List<Locality> listLocality = localityDao.getLocalityListByRegionIdByActive(region.getRegionId(),true);
			if(!listLocality.isEmpty())
				temp.add(region);
		}
		return temp;
	}
	@Transactional(readOnly = true)   
	public Region searchRegionByRegionId(Integer regionId){
		return regionDao.findRegionByRegionId(regionId);
	}

	
	@Transactional(readOnly = true)   
	public List<Locality> getListLocalityByRegionIdByActive(Integer regionId,boolean active){
		return localityDao.getLocalityListByRegionIdByActive(regionId, active);
	}
	@Transactional(readOnly = true)
	public Locality searchLocalityByRegionIdByLocalityId(Integer regionId ,Short localityId) {
	    return localityDao.findLocalityByRegionIdByLocalityId(regionId, localityId);
	}
	
	
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy() {
		return pharmacyDao.getListPharmacy();
	}
	@Transactional(readOnly = true)  
	public List<Pharmacy> getListPharmacyByPhapharmacyIdNull(boolean active){
		List<Pharmacy> temp = new ArrayList<Pharmacy>();
		List<Pharmacy> listPharmacy = pharmacyDao.getListPharmacy(active);
		for (Pharmacy pharmacy : listPharmacy) {
			if(pharmacy.getPhapharmacyId() == null)
				temp.add(pharmacy);
		}
		return temp;
	}
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy(boolean active) {
		return pharmacyDao.getListPharmacy(active);
	}
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy(Integer regionId) {
		return pharmacyDao.getListPharmacy(regionId);
	}
	@Transactional(readOnly = true)
	public List<Pharmacy> getListPharmacy(Integer regionId,boolean active) {
		return pharmacyDao.getListPharmacy(regionId, active);
	}
	@Transactional(readOnly = true)   
	public Pharmacy searchPharmacyByPharmacyId(Integer pharmacyId) {
		return pharmacyDao.findByPharmacyId(pharmacyId);
	}
	@Transactional(readOnly = true)
	public List<Pharmacy> getListPharmacyByPharmacyId(Integer pharmacyId,boolean active) {
		List<Pharmacy> listPharmacy = new ArrayList<Pharmacy>();
		listPharmacy.add(pharmacyDao.findByPharmacyId(pharmacyId));
		listPharmacy.addAll(pharmacyDao.getListPharmacyByPhaPharmacyIdByActive(pharmacyId, active));
		for (Pharmacy pharmacy : listPharmacy) {
			List<Userpharmacy> listUserPharamacy = userPharmacyDao.getListUserPharmacy(pharmacy.getPharmacyId(), active);
			for (Userpharmacy userpharmacy : listUserPharamacy){
				userpharmacy.setUser(userDao.findByUserId(userpharmacy.getUserpharmacyPK().getUserId()));
				userpharmacy.setTransactionCollection(transactionDao.getListTransactionByPharmacyIdByUserId(userpharmacy.getUserpharmacyPK().getPharmacyId(), userpharmacy.getUserpharmacyPK().getUserId()));
			}
			pharmacy.setUserpharmacyCollection(listUserPharamacy);
		}
		return listPharmacy;
	}
	@Transactional
	public void updatePharmacy(Pharmacy pharmacy) {
		pharmacyDao.update(pharmacy);
	}
	@Transactional
	public void removePharmacy(Pharmacy pharmacy) {
		pharmacyDao.remove(pharmacy);
	}
	@Transactional
	public void addPharmacy(Pharmacy pharmacy) {
		pharmacyDao.add(pharmacy);
	}	
	
	
	@Transactional
	public void addStock(Stock stock) {
		stockDao.add(stock);
	}

	
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStock() {
		return pharmacyStockDao.getListPharmacyStock();
	}
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStockByPharmacyId(Integer pharmacyId) {
		return pharmacyStockDao.getListPharmacyStockByPharmacyId(pharmacyId);
	}
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStockByStockId(Integer stockId) {
		return pharmacyStockDao.getListPharmacyStockByStockId(stockId);
	}
	@Transactional(readOnly = true)   
	public Pharmacystock searchPharmacyStockByPharmacyIdByStockId(Integer pharmacyId, Integer stockId) {
		return pharmacyStockDao.findByPharmacyIdByStockId(pharmacyId, stockId);
	}
	@Transactional
	public void updatePharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.update(pharmacyStock);
	}
	@Transactional
	public void removePharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.remove(pharmacyStock);
	}
	@Transactional
	public void addPharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.add(pharmacyStock);
	}
}
